﻿using System;
using System.Web;
using System.Web.Routing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Rhino.Mocks;
using YABE;

namespace YABETests
{
    
    /// <summary>
    /// Summary description for RoutingTableTest
    /// </summary>
    [TestClass]
    public class RoutingTableTest
    {
        public RoutingTableTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void JsonControllerRoutingTestShouldRoute_Json()
        {
            MockRepository mock = new  MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using(mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Json/");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData,"No route is found");
            Assert.IsTrue(String.IsNullOrEmpty(routeData.Values["action"].ToString()));
            Assert.IsTrue(String.IsNullOrEmpty(routeData.Values["id"].ToString()));
            Assert.AreEqual("Json",routeData.Values["controller"].ToString());
        }

        [TestMethod]
        public void JsonControllerRoutingTestShouldRoute_Json_Get()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Json/Get");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Get",routeData.Values["action"].ToString());
            Assert.AreEqual("Json", routeData.Values["controller"].ToString());
            Assert.IsTrue(String.IsNullOrEmpty(routeData.Values["id"].ToString()));
        }

        [TestMethod]
        public void JsonControllerRoutingShouldRoute_Json_Get_5()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Json/Get/5");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Get",routeData.Values["action"].ToString());
            Assert.AreEqual("5",routeData.Values["id"].ToString());
            Assert.AreEqual("Json", routeData.Values["controller"].ToString());
        }

        [TestMethod]
        public void AdminControllerActionShouldRoute_Login()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Login/");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Admin",routeData.Values["controller"].ToString());
            Assert.AreEqual("Login",routeData.Values["action"].ToString());
        }

        [TestMethod]
        public void PostControllerActionShouldRoute_Tag_uncategorized()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/tag/");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Posts",routeData.Values["controller"].ToString());
            Assert.AreEqual("ListByTag",routeData.Values["action"].ToString());
            Assert.AreEqual("uncategorized",routeData.Values["tag"].ToString());
        }

        [TestMethod]
        public void PostControllerActionShouldRoute_tag_mvc()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/tag/mvc");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Posts", routeData.Values["controller"].ToString());
            Assert.AreEqual("ListByTag", routeData.Values["action"].ToString());
            Assert.AreEqual("mvc", routeData.Values["tag"].ToString());
        }

        [TestMethod]
        public void PostControllerShouldRoute_Posts_List()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Posts/");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Posts",routeData.Values["controller"].ToString());
            Assert.AreEqual("List",routeData.Values["action"].ToString());
            Assert.IsTrue(String.IsNullOrEmpty(routeData.Values["id"].ToString()));
        }

        [TestMethod]
        public void PostControllerShouldRoute_Posts_List_5()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Posts/List/5");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Posts", routeData.Values["controller"].ToString());
            Assert.AreEqual("List", routeData.Values["action"].ToString());
            Assert.AreEqual("5", routeData.Values["id"].ToString());
        }

        [TestMethod]
        public void PostControllerShouldRoute_Posts_Get()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Posts/Get/");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Posts", routeData.Values["controller"].ToString());
            Assert.AreEqual("Get", routeData.Values["action"].ToString());
            Assert.IsTrue(String.IsNullOrEmpty(routeData.Values["id"].ToString()));
        }

        [TestMethod]
        public void PostControllerShouldRoute_Posts_Get_5()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Posts/Get/5");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Posts", routeData.Values["controller"].ToString());
            Assert.AreEqual("Get", routeData.Values["action"].ToString());
            Assert.AreEqual("5", routeData.Values["id"].ToString());
        }

        [TestMethod]
        public void AdminControllerShouldRoute_Admin_List()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Admin/");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Admin", routeData.Values["controller"].ToString());
            Assert.AreEqual("List", routeData.Values["action"].ToString());
            Assert.IsTrue(String.IsNullOrEmpty(routeData.Values["id"].ToString()));
        }

        [TestMethod]
        public void AdminControllerShouldRoute_Admin_List_5()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Admin/List/5");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Admin", routeData.Values["controller"].ToString());
            Assert.AreEqual("List", routeData.Values["action"].ToString());
            Assert.AreEqual("5", routeData.Values["id"].ToString());
        }

        [TestMethod]
        public void AdminControllerShouldRoute_Admin_Get()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Admin/Get");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Admin", routeData.Values["controller"].ToString());
            Assert.AreEqual("Get", routeData.Values["action"].ToString());
            Assert.IsTrue(String.IsNullOrEmpty(routeData.Values["id"].ToString()));
        }

        [TestMethod]
        public void AdminControllerShouldRoute_Admin_Get_5()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Admin/Get/5");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Admin", routeData.Values["controller"].ToString());
            Assert.AreEqual("Get", routeData.Values["action"].ToString());
            Assert.AreEqual("5", routeData.Values["id"].ToString());
        }

        [TestMethod]
        public void FeedsControllerShouldRoute_Feeds()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Feeds/");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Feeds", routeData.Values["controller"].ToString());
            Assert.AreEqual("rss20", routeData.Values["action"].ToString());
        }

        [TestMethod]
        public void FeedsControllerShouldRoute_Atom()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Feeds/Atom");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Feeds", routeData.Values["controller"].ToString());
            Assert.AreEqual("Atom", routeData.Values["action"].ToString());
        }

        [TestMethod]
        public void FeedShouldRoute_Feeds()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Feed/");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Feeds", routeData.Values["controller"].ToString());
            Assert.AreEqual("rss20", routeData.Values["action"].ToString());
        }

        [TestMethod]
        public void FeedShouldRoute_Atom()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/Feed/Atom");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Feeds", routeData.Values["controller"].ToString());
            Assert.AreEqual("Atom", routeData.Values["action"].ToString());
        }

        
        [TestMethod]
        public void PostControllerShouldRoute2_ListYear()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/2008/");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Posts", routeData.Values["controller"].ToString());
            Assert.AreEqual("ListYear", routeData.Values["action"].ToString());
            Assert.AreEqual("2008", routeData.Values["year"].ToString());
            
        }

        [TestMethod]
        public void PostControllerShouldRoute2_ListYearMonth()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/2008/09");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Posts", routeData.Values["controller"].ToString());
            Assert.AreEqual("ListMonth", routeData.Values["action"].ToString());
            Assert.AreEqual("2008",routeData.Values["year"].ToString());
            Assert.AreEqual("09",routeData.Values["month"].ToString());
        }

        [TestMethod]
        public void PostControllerShouldRoute2_ListYearMonthDay()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/2008/12/05");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Posts", routeData.Values["controller"].ToString());
            Assert.AreEqual("ListDay", routeData.Values["action"].ToString());
            Assert.AreEqual("2008", routeData.Values["year"].ToString());
            Assert.AreEqual("12", routeData.Values["month"].ToString());
            Assert.AreEqual("05",routeData.Values["day"].ToString());
        }

        [TestMethod]
        public void PostControllerShouldRoute2_ListYearMonthDayTitle()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/2008/12/05/MVC Preview 5");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Posts", routeData.Values["controller"].ToString());
            Assert.AreEqual("Single", routeData.Values["action"].ToString());
            Assert.AreEqual("2008", routeData.Values["year"].ToString());
            Assert.AreEqual("12", routeData.Values["month"].ToString());
            Assert.AreEqual("05", routeData.Values["day"].ToString());
            Assert.AreEqual("MVC Preview 5",routeData.Values["title"].ToString());
        }

        [TestMethod]
        public void UnknownControllerShouldRoute2_PostsList()
        {
            MockRepository mock = new MockRepository();
            RouteCollection routes = new RouteCollection();
            GlobalApplication.RegisterRoutes(routes);
            HttpContextBase httpContextBase;

            using (mock.Record())
            {
                httpContextBase = mock.HttpContextFactory("/xyzaaa/");
            }
            RouteData routeData = routes.GetRouteData(httpContextBase);
            Assert.IsNotNull(routeData, "No route is found");
            Assert.AreEqual("Posts", routeData.Values["controller"].ToString());
            Assert.AreEqual("Index",routeData.Values["action"].ToString());    
        }

        
    }
}
